layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var util = layui.util;
    var admin = layui.admin;


    //渲染表格
    var authUserTableIns = table.render({
        elem: '#authUserTable',
        url: prefix + '/authUser/allocatedList',
        initSort: {
            field: 'gmtCreate'
            , type: 'desc'
        },
        where: {
            fieldName: 'gmtCreate',
            sortType: 'desc',
            roleId: currentRole.roleId
        },
        toolbar: ['<p>',
            '<button lay-event="auth" class="layui-btn layui-btn-sm layui-bg-cyan icon-btn ' + editFlag + '"><i class="layui-icon layui-icon-add-1"></i>添加用户</button>',
            '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn ' + removeFlag + '"><i class="layui-icon layui-icon-delete"></i>批量删除授权</button>',
            '</p>'].join(''),
        cols: [
            [
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'loginName', title: '登录账号', sort: true},
                {field: 'userName', title: '用户名称', sort: true},
                {field: 'email', title: '邮箱', sort: true},
                {field: 'phoneNumber', title: '手机号'},
                {
                    field: 'gmtCreate', title: '创建时间', width: 180, templet: function (d) {
                        return util.toDateString(d.gmtCreate);
                    }, sort: true
                },
                {title: '操作', toolbar: '#authUserTbBar', align: 'center', minWidth: 400}
            ]
        ]
    });

    //表格搜索
    form.on('submit(authUserTbSearch)', function (data) {
        authUserTableIns.reload({where: data.field, page: {curr: 1}});
        return false;
    });

    //表格工具条点击事件
    table.on('tool(authUserTable)', function (obj) {
        var data = obj.data;
        if (obj.event === 'cancel') {
            deleteTable([data.userId]);
        }
    });

    //表头工具栏点击事件
    table.on('toolbar(authUserTable)', function (obj) {
        var data = table.checkStatus('authUserTable').data;
        if (obj.event === "auth") {
            authUser();
        } else if (obj.event === "del") {
            data = data.map(function (item) {
                return item.userId;
            });
            deleteTable(data);
        }
    });

    //触发排序事件
    table.on('sort(authUserTable)', function (obj) {
        authUserTableIns.reload({
            initSort: obj
            , where: {
                fieldName: obj.field
                , sortType: obj.type
                ,roleId: currentRole.roleId
            }
        });
    });


    // 导入新的用户结构
    function authUser() {
        var importTableUrl = prefix + "/authUser/selectUser/" + currentRole.roleId;
        var height = $(window).height() - 200;
        layer.open({
            type: 2,
            area: ['840px', height + 'px'],
            offset: 'auto',
            fix: false,
            maxmin: true,
            shade: 0.3,
            title: "导入表",
            content: importTableUrl,
            btn: ['确定', '关闭'],
            // 弹层外区域关闭
            shadeClose: true,
            yes: function (index, layero) {
                // var body = layer.getChildFrame('body', index);
                var iframeWin = window[layero.find('iframe')[0]['name']];
                admin.req(prefix + "/authUser/selectAll", {
                    roleId: currentRole.roleId,
                    userIds: iframeWin.userImportSelect
                }, function (resp) {
                    himio.notice.resp(resp, function () {
                        authUserTableIns.reload({page: {curr: 1}});
                    });
                    layer.close(index);
                }, "post");
            },
            cancel: function (index) {
                layer.close(index);
                return true;
            }
        });
    }

    //表格顶部工具栏删除
    function deleteTable(data) {
        if (data.length === 0) {
            himio.notice.warn('请选择要取消授权的用户');
            return;
        }
        admin.confirm("确认要取消授权选中的" + data.length + "个用户吗?", function (index) {
            admin.req(prefix + "/authUser/cancelAll", {
                roleId: currentRole.roleId,
                userIds: data.join(",")
            }, function (resp) {
                himio.notice.resp(resp, function () {
                    authUserTableIns.reload({where: data.field, page: {curr: 1}});
                });
                layer.close(index);
            }, "post");
        });
    }


});